package com.tueye.utils.servlet;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.tueye.cache.TuEyeCache;
import com.tueye.entity.Sys_glob_classMap;
import com.tueye.service.IBuilderHtml;
import com.tueye.service.ITopicService;
import com.tueye.service.exception.TuEyeServiceException;
import com.tueye.service.global.IGlobalGridService;
import com.tueye.utils.TuEyeConstant;

public class TueyeInit extends HttpServlet {
	protected Log log = LogFactory.getLog(TueyeInit.class);
	
	TuEyeCache cache = null;

	/**
	 * Constructor of the object.
	 */
	public TueyeInit() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	@Override
	public void destroy() {
		// clear the tueyecache
		cache.clear();

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	@Override
	public void init() throws ServletException {
		try {
			log.info("the webserver initialization the tueyeservelt...");
			TuEyeConstant.ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
			cache = (TuEyeCache) TuEyeConstant.ctx.getBean("tueyecache");
			cache.initCash();
			log.info("the webserver initialization the project's path...");
			String projectPath = getServletContext().getRealPath("/");
			TuEyeConstant.PROJECTPATH = projectPath;
			TuEyeConstant.PROJECT_ADMIN_PATH = projectPath + File.separatorChar+ "pages" + File.separatorChar + "admin"+ File.separatorChar;
			TuEyeConstant.PROJECT_TOPIC_PATH = projectPath + File.separatorChar+ "pages" + File.separatorChar + "topics"+ File.separatorChar;

			
			   ITopicService topicservice = (ITopicService)TuEyeConstant.ctx.getBean("topicService"); 
			   log.info("check the fold whether exist..."); 
			   topicservice.checkTopicTypeFolds();
			 
			/**
			 * add by david
			 * 系统启动时候初始化创建各个频道页面
			 */
			IBuilderHtml builderTemplateServie = (IBuilderHtml)TuEyeConstant.ctx.getBean("builderTemplateServie");
			builderTemplateServie.buildHtml();
			
			
			/**================================
			* 初始化class映射的MAP
			*===================================
			*/
			
			Sys_glob_classMap classMap = new Sys_glob_classMap();
			String hql = "from Sys_glob_classMap";
			IGlobalGridService globalGridService = (IGlobalGridService)TuEyeConstant.ctx.getBean("globalGridService");
			List <Sys_glob_classMap> list = globalGridService.getclassMap(hql);
			if(list.size()>0){
			for(int i=0;i<list.size();i++){
				classMap = (Sys_glob_classMap)list.get(i);
				TuEyeConstant.CLASS_MAPPING.put(classMap.getClassID(), classMap.getClassStr());
			}
			}
			
		} catch (TuEyeServiceException e) {
			e.printStackTrace();
		}
	}

}
